import 'package:flutter/material.dart';
import 'package:flutter_module/views/mapFragmentWidgets/map_title_bar.dart';

///分割线回调函数
// typedef SeparatorWidgetFunction = Widget Function(
//     BuildContext context, int index);

///item构建函数
// typedef ItemWidgetsFunction = List<Widget> Function();

abstract class BasePage {
  ///抽屉页面UI
  BasePageWidget? _widget;

  ///顶部状态栏UI
  MapTitleBarAdapter? _adapter;

  ///构建每个item
  List<Widget> createWidgets(BuildContext context);

  ///构建顶部状态栏
  MapTitleBarAdapter createMapTitleBarAdapter(BuildContext context);

  ///销毁接口
  void dispose();

  void update();

  ///给抽屉页面的布局
  Widget getWidget(BuildContext context, ScrollController controller) {
    if (_widget == null) {
      _widget = BasePageWidget(
        key: ValueKey(hashCode),
        controller: controller,
        widgetList: createWidgets(context),
        // separatorWidget: separatorWidget,
      );
      return _widget!;
    } else {
      return _widget!;
    }
  }

  ///提供给地图顶部状态的布局
  MapTitleBarAdapter getMapTitleBarAdapter(BuildContext context) {
    _adapter ??= createMapTitleBarAdapter(context);
    return _adapter!;
  }

  ///增加属性栏
  void addItem(Widget itemWidget, {int? index}) {
    if (_widget != null) {
      _widget!.addItem(itemWidget, index: index);
    }
  }

  ///删除属性栏
  void removeItem({Widget? itemWidget, int? index}) {
    if (_widget != null) {
      _widget!.removeItem(itemWidget: itemWidget, index: index);
    }
  }

  void removeAllItem() {
    if (_widget != null) {
      _widget!.removeAllItems();
    }
  }
}

class BasePageWidget extends StatefulWidget {
  ///控制滑动的，由父控制，子控制要用[NeverScrollableScrollPhysics]限制
  final ScrollController controller;

  ///子项集合
  final List<Widget> widgetList;

  final GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>();

  BasePageWidget({Key? key, required this.controller, required this.widgetList})
      : super(key: key);

  @override
  State<BasePageWidget> createState() => _BasePageWidgetState();

  ///动态增加抽屉页面的列表内容项
  void addItem(Widget itemWidget, {int? index}) {
    if (index == null || index > widgetList.length) {
      index = widgetList.length;
    }
    widgetList.insert(index, itemWidget);
    _listKey.currentState?.insertItem(index);
  }

  ///动态删除抽屉页面的列表内容项
  void removeItem({Widget? itemWidget, int? index}) {
    if (itemWidget != null) {
      index ??= widgetList.length - 1;
      for (int i = 0; i < widgetList.length; i++) {
        if (widgetList[i] == itemWidget) {
          _listKey.currentState?.removeItem(
              i, (context, animation) => _buildItem(widgetList[i], animation));
          widgetList.removeAt(i);
          break;
        }
      }
    } else if (index != null && index < widgetList.length) {
      var item = widgetList[index];
      _listKey.currentState?.removeItem(
          index, (context, animation) => _buildItem(item, animation));
      widgetList.removeAt(index);
    } else {
      index ??= widgetList.length - 1;
      var item = widgetList[index];
      _listKey.currentState?.removeItem(
          index, (context, animation) => _buildItem(item, animation));
      widgetList.removeAt(index);
    }
  }

  void removeAllItems() {
    for (int i = widgetList.length - 1; i >= 0; i--) {
      Widget widget = widgetList[i];
      _listKey.currentState?.removeItem(
          i, (context, animation) => _buildItem(widget, animation));
    }
    widgetList.clear();
  }

  ///动态修改抽屉页面列表子项时的动画效果
  Widget _buildItem(Widget _item, Animation<double> _animation) {
    return ScaleTransition(
      scale: _animation,
      child: _item,
    );
    // return SlideTransition(
    //     position: _animation.drive(CurveTween(curve: Curves.easeIn)).drive(
    //         Tween<Offset>(begin: const Offset(1, 1), end: const Offset(0, 1))),
    //     child: _item);
  }
}

///
class _BasePageWidgetState extends State<BasePageWidget> {
  @override
  Widget build(BuildContext context) {
    return AnimatedList(
        key: widget._listKey,
        controller: widget.controller,
        shrinkWrap: true,
        padding: const EdgeInsets.only(left: 20, right: 20),
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (context, index, animation) {
          return ScaleTransition(
              scale: animation, child: widget.widgetList[index]);
        },
        initialItemCount: widget.widgetList.length);
  }
}
